﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Clock alarmClock = new Clock
            {
                AlarmTime = DateTime.Now.AddSeconds(5)
            };
            alarmClock.Tick += (sender, e) =>
                Console.WriteLine($"嘀嗒... 当前时间：{e.CurrentTime:HH:mm:ss}");
            alarmClock.Alarm += (sender, e) =>
                Console.WriteLine($"\a响铃！！！预设时间 {e.AlarmTime:HH:mm:ss} 到啦！");

            Console.WriteLine($"闹钟已启动，将在 {alarmClock.AlarmTime:HH:mm:ss} 响铃");
            alarmClock.Start(); 

            Console.ReadKey();
        }
        public class ClockEventArgs : EventArgs
        {
            public DateTime CurrentTime { get; set; }
            public DateTime AlarmTime { get; set; }
        }
        public class Clock
        {
            public DateTime AlarmTime { get; set; }
            public event EventHandler<ClockEventArgs> Tick;
            public event EventHandler<ClockEventArgs> Alarm;
            public void Start()
            {
                Console.WriteLine("闹钟开始运行...");

                while (DateTime.Now < AlarmTime)
                {
                    Thread.Sleep(1000);
                    OnTick();
                }
                OnAlarm();
            }

            protected virtual void OnTick()
            {
                Tick?.Invoke(this, new ClockEventArgs
                {
                    CurrentTime = DateTime.Now,
                    AlarmTime = this.AlarmTime
                });
            }
            protected virtual void OnAlarm()
            {
                Alarm?.Invoke(this, new ClockEventArgs
                {
                    CurrentTime = DateTime.Now,
                    AlarmTime = this.AlarmTime
                });
            }
        }
    }
}
